﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class Helper {
    #region constField
    public const string subAnimation = "Animation";
    public const string subShadow = "Shadow";
    #endregion
    #region ReferanceHelper


    /// <summary>
    /// Jumper帮助。帮助判空。从子物体寻找指定名字的transform
    /// </summary>
    /// <param name="mono">需要判空的脚本</param>
    /// <param name="anm">需要判空的对象</param>
    /// <param name="exMessage">找不到时抛出的异常</param>
    [System.Diagnostics.Conditional("debug")]
    public static void ProbeJumperAnm(this MonoBehaviour mono, ref Transform anm, string name, bool isThrow, string exMessage)
    {
        if (anm == null)
        {
            MonoBehaviour.print(mono.name + "don't get transform." + exMessage);
            anm = mono.transform.Find(name);
            if (anm == null)
            {
                MonoBehaviour.print("error" + mono.name + "can't get transform." + exMessage);
                if (isThrow)
                {
                    throw new PrefabException(exMessage);

                }

            }
        }
    }

    /// <summary>
    /// 控件判空帮助工具。mono中需要判空控件probe
    /// </summary>
    /// <typeparam name="T">控件类型</typeparam>
    /// <param name="mono">需要判空的脚本</param>
    /// <param name="probe">被判空的控件</param>
    /// <param name="exMessage">找不到时抛出异常</param>
    [System.Diagnostics.Conditional("debug")]
    public static void ProbeComponent<T>(this MonoBehaviour mono, ref T probe, string exMessage) where T : Component
    {
        if (probe == null)
        {
            MonoBehaviour.print(mono.name + "don't get Component.(1)" + exMessage);
            probe = mono.GetComponent<T>();
            if (probe == null)
            {
                MonoBehaviour.print(mono.name + "don't get Component.(2)" + exMessage);
                probe = mono.GetComponentInChildren<T>();
                if (probe == null)
                {
                    throw new PrefabException(exMessage);

                }
            }

        }



    }

    /// <summary>
    /// 根据名字和tag来找游戏物体赋值给gmObj字段
    /// </summary>
    /// <param name="mono">需要给字段赋值的mono</param>
    /// <param name="gmObj">需要赋值的字段</param>
    /// <param name="objName">名字</param>
    /// <param name="objTag">tag</param>
    /// <param name="nameEx">名字找不到时显示的错误</param>
    /// <param name="tagEx">tag也找不到时显示的错误</param>
    [System.Diagnostics.Conditional("debug")]
    public static void ProbeGameObj(this MonoBehaviour mono, ref GameObject gmObj, string objName, string objTag, string nameEx, string tagEx)
    {
        if (gmObj == null)
        {
            gmObj = GameObject.Find(objName);
            if (gmObj == null)
            {
                MonoBehaviour.print(nameEx);
                gmObj = GameObject.FindGameObjectWithTag(objTag);
                if (gmObj == null)
                {
                    MonoBehaviour.print(tagEx);
                    throw new PrefabException("No GameObj", mono.name, objName);
                }
            }

        }
    }

    /// <summary>
    /// 判空。isThrow则抛异常
    /// </summary>
    /// <param name="obj">判空的obj</param>
    /// <param name="isThrow">是否抛异常</param>
    /// <param name="ex">显示的错误信息</param>
    [System.Diagnostics.Conditional("debug")]
    public static void ProbeIsNull<T>(this MonoBehaviour mono, T obj, bool isThrow, string ex)
    {
        if (obj == null)
        {
            MonoBehaviour.print(ex);
            if (isThrow)
            {
                throw new PrefabException(ex);

            }
        }
    }

    #endregion

    #region 调试助手
    [System.Diagnostics.Conditional("debug")]
    public static void DebugPrint(System.Object s)
    {
        MonoBehaviour.print(s.ToString());
    }
    #endregion

    /// <summary>
    /// 是否是空指针
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static bool IsNull(this Object obj)
    {
        if (obj==null)
        return true;
        return false;
    }


    #region TagRule
    /// <summary>
    /// tag校验位
    /// 第一位分别是E:entity,S:shots,M:map,U:unsigned,V:eventObj,.
    /// 对Entity第二位，P:player,M:monster,N:NPC,
    /// 对eventObj第二位，C:colliderObj,T:triggerObj
    /// 对Shots，第二位,R:lazey(ray)
    /// 对map先留空
    /// 校验位默认至少4位
    /// 留空可以用X。
    /// </summary>
    /*
#region tag_type
#region 第一位
public const int MAX_NUMBERS = 4;//至少4位校验位
        public const string ENTITY = "E";
        public const string MAP = "M";
        public const string SHOTS = "S";
        public const string EVENTOBJ = "V";
        public const string UNSIGNED = "U";
        #endregion

        #region 第二位
            //Entity
            public const string PLAYER = "EP";
            public const string MONSTER = "EM";
            public const string NPC = "EN";
            //EvnetObj
            public const string COLLIDEROBJ = "VC";
            public const string TRIGGEROBJ = "VT";
            //Shots
            public const string LAZEY = "SR";  
        #endregion
    #endregion
*/
    #endregion
}
